package com.liu.conversionNumber;

import java.util.Scanner;

public class BinaryToDecimal {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个二进制数：");
        String binaryString = sc.nextLine();
        int length = binaryString.length();
        int decimal = 0;
        int power = 0;
        for (int i = length - 1; i >= 0; i--) {
            char c = binaryString.charAt(i);
            System.out.println(c);
            // 返回指定的 Unicode 字符表示的int值
            int digit = Character.getNumericValue(binaryString.charAt(i));
            decimal += digit * Math.pow(2,power);
            power++;
        }
        System.out.println("转换为十进制后：" + decimal);
    }

}
